from typing import List


class Solution:
    def intersection(self, start1: List[int], end1: List[int], start2: List[int], end2: List[int]) -> List[float]:
        x1, y1, x2, y2, x3, y3, x4, y4 = (*start1, *end1, *start2, *end2)
        x_min = max(min(x1, x2), min(x3, x4))
        x_max = min(max(x1, x2), max(x3, x4))
        # 定义域不重合
        if x_min > x_max:
            return []

        a1 = y2 - y1
        b1 = x1 - x2
        c1 = x2 * y1 - x1 * y2

        a2 = y4 - y3
        b2 = x3 - x4
        c2 = x4 * y3 - x3 * y4

        # 平行
        if a1 * b2 == a2 * b1:
            if a1 != 0:
                t1, t2 = a1, a2
            else:
                t1, t2 = b1, b2
            # 不重合
            if t1 * c2 != t2 * c1:
                return []
            # 重合
            # 重合且垂直
            if b1 == 0:
                y_min = max(min(y1, y2), min(y3, y4))
                y_max = min(max(y1, y2), max(y3, y4))
                if y_min > y_max:
                    return []
                return [x_min, y_min]
            # 重合不垂直
            y = -(a1 * x_min + c1) / b1
            return [x_min, y]
        # 不平行
        x = -(c1 * b2 - c2 * b1) / (a1 * b2 - a2 * b1)
        if x_min <= x <= x_max:
            if b1 != 0:
                a, b, c = a1, b1, c1
            else:
                a, b, c = a2, b2, c2
            y = -(a * x + c) / b
            return [x, y]
        return []
